package com.stu.wgt.test.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stu.wgt.test.bean.Payment;
import com.stu.wgt.test.result.R;
import com.stu.wgt.test.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wgt
 * @since 2024-02-04
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    PaymentService paymentService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public R addPayment(@RequestBody Payment payment) {
        System.out.println(payment);
        int result = paymentService.insert(payment);
        if (result == 1) {
            return R.ok().data("msg", "增加成功");
        }
        return R.error().data("msg", "增加失败");
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public R deletePayment(@PathVariable("id") Integer id) {
        int result = paymentService.delete(id);
        if (result == 1) {
            return R.ok().data("msg", "删除成功");
        }
        return R.error().data("msg", "删除失败");
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public R updatePayment(@PathVariable("id") Integer id, @RequestBody Payment payment) {
        payment.setId(id);
        int result = paymentService.update(payment);
        if (result == 1) {
            return R.ok().data("msg", "更新成功");
        }
        return R.error().data("msg", "更新失败");
    }

    @RequestMapping(value = "/selectById/{id}", method = RequestMethod.GET)
    public R selectById(@PathVariable("id") Integer id) {
        try {
            List<Payment> paymentList = paymentService.selectById(id);
            return R.ok().data("payment", paymentList);
        } catch (Exception e) {
            return R.error().data("msg", "查询失败");
        }
    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public R searchPayment(
            @RequestParam("query") String query,
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize
    ) {
        try {
            IPage<Payment> paymentIPage = paymentService.searchPayment(query, pageNum, pageSize);
            return R.ok().data("payment", paymentIPage.getRecords()).data("total", paymentIPage.getTotal());
        } catch (Exception e) {
            return R.error().data("msg", "查询失败，请更新查询参数");
        }
    }
}

